#include <cmath>

#include <QtCore/QDebug>

#include "circletimeprogress.h"

CircleTimeProgress::CircleTimeProgress(QQuickItem *parent) :
    QQuickPaintedItem(parent),
    m_depth (0),
    m_color (Qt::red),
    m_step (10),
    m_period (0),
    m_clocs (0)
{
}

void CircleTimeProgress::paint(QPainter *painter)
{
    if(!m_period || !m_step)
    {
        return;
    }

    const qreal pi_2 = 90;
    const qreal pi = 180;

    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setBrush(m_color);
    painter->setPen(QPen(m_borderColor,m_depth));

    const int sectors = m_period/m_step;
    const int angle = 2*pi/sectors;

    const int recHeight = height();
    const int recWidth = width();

    int currentSector = m_clocs / m_step;

    for(int sector = 0; sector<currentSector;++sector)
    {
       const qreal startAngle = pi_2 + angle*sector;
       painter->drawPie(0,0, recWidth,recHeight,startAngle*16, angle*16);
    }
}

int CircleTimeProgress::depth () const
{
    return m_depth;
}
void CircleTimeProgress::setDepth (int depth)
{
    m_depth = depth;
    emit depthChanged();
    update ();
}

QColor CircleTimeProgress::color () const
{
    return m_color;
}

void CircleTimeProgress::setColor (const QColor& color)
{
    m_color = color;
    emit colorChanged();
    update ();
}

QColor CircleTimeProgress::borderColor () const
{
    return m_borderColor;
}
void CircleTimeProgress::setBorderColor (const QColor& color)
{
    m_borderColor = color;
    emit borderColorChanged ();
}

int CircleTimeProgress::step () const
{
    return m_step;
}
void CircleTimeProgress::setStep (int step)
{
    m_step = step;
    emit stepChanged();
    update ();
}

int CircleTimeProgress::period () const
{
    return m_period;
}
void CircleTimeProgress::setPeriod (int period)
{
    m_period = period;
    emit periodChanged();
    update ();
}

int CircleTimeProgress::clocs () const
{
    return m_clocs;
}

void CircleTimeProgress::setClocs (int clocs)
{
    m_clocs = clocs;
    emit clocsChanged();

    if(m_clocs<=m_period){
        update ();
    }
}
